#include <stdio.h>
#include <conio.h>
#include <curl/curl.h>
#include <cJSON.h>
#include <SDL2/SDL.h>
#undef main

typedef size_t (*CurlCallback)(char *, size_t, size_t, void *);

void my_curl(char *url, CurlCallback callback)
{
    CURL *curl;
    CURLcode res;

    // 初始化libcurl
    curl_global_init(CURL_GLOBAL_DEFAULT);

    // 创建一个新的CURL对象
    curl = curl_easy_init();
    if (curl)
    {
        // struct curl_slist *headers = NULL;
        // headers = curl_slist_append(headers, "Accept-Charset: utf-8");
        // curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);

        // 执行请求
        res = curl_easy_perform(curl);

        // 检查是否有错误发生
        if (res != CURLE_OK)
        {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        }
        // 清理CURL对象
        curl_easy_cleanup(curl);
    }

    // 结束libcurl的使用
    curl_global_cleanup();
}

typedef struct
{
    char *desc;
    int id;
    char *imagePath;
    int isVisible;
    int order;
    char *title;
    int type;
    char *url;
} Data;

typedef struct
{
    int errorCode;
    char *errorMsg;
    Data **data; // 指针数组
} Banner;

Banner *banner = NULL;

size_t banner_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{

    cJSON *root = cJSON_Parse(ptr);
    cJSON *data = cJSON_GetObjectItem(root, "data");
    int array_size = cJSON_GetArraySize(data);

    banner = (Banner *)malloc(sizeof(Banner));
    banner->errorCode = cJSON_GetNumberValue(cJSON_GetObjectItem(root, "errorCode"));
    banner->errorMsg = cJSON_GetStringValue(cJSON_GetObjectItem(root, "errorMsg"));
    banner->data = (Data **)malloc(array_size * sizeof(Data));

    int i;
    for (i = 0; i < array_size; i++)
    {
        cJSON *data_i = cJSON_GetArrayItem(data, i);
        banner->data[i] = (Data *)malloc(sizeof(Data));
        banner->data[i]->desc = cJSON_GetStringValue(cJSON_GetObjectItem(data_i, "desc"));
        banner->data[i]->id = cJSON_GetNumberValue(cJSON_GetObjectItem(data_i, "id"));
        banner->data[i]->imagePath = cJSON_GetStringValue(cJSON_GetObjectItem(data_i, "imagePath"));
        banner->data[i]->isVisible = cJSON_GetNumberValue(cJSON_GetObjectItem(data_i, "isVisible"));
        banner->data[i]->order = cJSON_GetNumberValue(cJSON_GetObjectItem(data_i, "order"));
        banner->data[i]->title = cJSON_GetStringValue(cJSON_GetObjectItem(data_i, "title"));
        banner->data[i]->type = cJSON_GetNumberValue(cJSON_GetObjectItem(data_i, "type"));
        banner->data[i]->url = cJSON_GetStringValue(cJSON_GetObjectItem(data_i, "url"));
    }

    printf("%s \n", banner->data[2]->imagePath);

    free(banner->data[0]);
    free(banner->data[1]);
    free(banner->data[2]);
    free(banner);

    cJSON_Delete(root);
    return nmemb;
}

int main()
{
    char *banner_api = "https://www.wanandroid.com/banner/json";
    my_curl(banner_api, banner_callback);

    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window *window = SDL_CreateWindow("SDL2 Window",
                                          SDL_WINDOWPOS_UNDEFINED,
                                          SDL_WINDOWPOS_UNDEFINED,
                                          640,
                                          480,
                                          SDL_WINDOW_OPENGL);
    
    SDL_Delay(4000);
    SDL_DestroyWindow(window);
    SDL_Quit(); // 退出SDL调用

    return 0;
}